package org.wheel.plugins.toolkit.sql.segment;

import org.wheel.plugins.toolkit.sql.core.AbstractWrapper;
import org.wheel.plugins.toolkit.sql.enums.CompareType;
import org.wheel.plugins.toolkit.sql.enums.LogicalOp;
import org.wheel.plugins.toolkit.sql.helper.ColumnRef;

import java.util.List;
import java.util.stream.Collectors;

/**
 * 多列比较： (col1, col2) > (?, ?)
 */
public final class MultiColumnWhereSegment extends AbstractWhereSegment {

    private final List<ColumnRef> columnSqls;   // t.dept_id, t.score
    private final List<Object> values;       // 10, 90
    private final CompareType operator;           // ">" 或 ">="

    public MultiColumnWhereSegment(LogicalOp logicalOp,
                                   List<ColumnRef> columnSqls,
                                   List<Object> values,
                                   CompareType operator,
                                   AbstractWrapper<?, ?> wrapper) {
        super(logicalOp, wrapper);
        this.columnSqls = columnSqls;
        this.values = values;
        this.operator = operator;
    }

    @Override
    public String getSql() {
        // 1. 生成参数占位符
        List<String> paramKeys = values.stream().map(value -> this.getWrapper().getContext().getParamBank().nextParam(value))
                .toList();
        // 2. 拼成 (col1, col2) > (:p0, :p1)

        String cols = "(" + columnSqls.stream().map(ColumnRef::getSql).collect(Collectors.joining(", ")) + ")";
        String params = "(" + paramKeys.stream().map(k -> ":" + k).collect(Collectors.joining(", ")) + ")";
        return cols + " " + operator.getOp() + " " + params;
    }
}